package com.loong.blog.controller;

import cn.hutool.core.bean.BeanUtil;
import com.loong.blog.fomatter.IdType;
import com.loong.blog.handler.exp.IdTypeException;
import com.loong.blog.model.dto.ArticleDTO;
import com.loong.blog.model.param.ArticleParam;
import com.loong.blog.model.po.ArticlePO;
import com.loong.blog.model.vo.ArticleVO;
import com.loong.blog.service.ArticleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author chenglong
 * @date 2025/5/9 16:20
 */
@Controller
@RequiredArgsConstructor
public class ArticleController {

    private final ArticleService articleService;

    @GetMapping(value = {"/", "/article/hot"})
    public String showHotArticle(Model model) {
        List<ArticlePO> articlePOList = articleService.queryTopArticle();
        // 转换为VO类型
        List<ArticleVO> articleVOList = BeanUtil.copyToList(articlePOList, ArticleVO.class);
        // 添加数据
        model.addAttribute("articleList", articleVOList);
        // 视图
        return "/blog/articleList";
    }

    @PostMapping(value = "/article/add")
    public String addArticle(@Validated(ArticleParam.AddArticle.class) ArticleParam param) {
        ArticleDTO articleDTO = new ArticleDTO();
        articleDTO.setContent(param.getContent());
        articleDTO.setTitle(param.getTitle());
        articleDTO.setSummary(param.getSummary());
        boolean add = articleService.addArticle(articleDTO);
        return "redirect:/article/hot";
    }

    @GetMapping("/article/get")
    public String queryById(Integer id, Model model) {
        if (id != null && id > 0) {
            ArticleDTO articleDTO = articleService.queryById(id);
            // 转为VO类型
            ArticleVO articleVO = BeanUtil.copyProperties(articleDTO, ArticleVO.class);
            model.addAttribute("article", articleVO);
            return "/blog/editArticle";
        } else {
            return "/blog/error/error";
        }
    }

    @PostMapping("/article/edit")
    public String modifyArticle(@Validated(ArticleParam.EditArticle.class) ArticleParam param) {
        ArticleDTO articleDTO = new ArticleDTO();
        articleDTO.setContent(param.getContent());
        articleDTO.setTitle(param.getTitle());
        articleDTO.setSummary(param.getSummary());
        articleDTO.setId(param.getId());
        boolean edit = articleService.editArticle(articleDTO);
        return "redirect:/article/hot";
    }

    @PostMapping("/article/remove")
    public String deleteArticle(@RequestParam("ids") IdType idType) {
        if (idType == null) {
            throw new IdTypeException("Id为空");
        }
        boolean delete = articleService.removeArticle(idType.getIdList());
        return "redirect:/article/hot";
    }

    @GetMapping("/article/detail/view")
    @ResponseBody
    public String viewArticleDetail(Integer id) {
        String top20Content = "无ID";
        if (id != null && id > 0) {
            top20Content = articleService.queryTop20Content(id);
        }
        return top20Content;
    }
}
